package utils

import (
	"encoding/json"
	"runtime/debug"
	"signal/lib/trace_log"
	"strings"
)

func Throw(err error) {
	if err != nil {
		panic(err)
	}
}

func ToJsonString(data interface{}) string {
	return string(toJson(data))
}

func ToJsonBytes(data interface{}) []byte {
	return toJson(data)
}

func toJson(data interface{}) []byte {
	if data == nil {
		return nil
	}
	marshal, _ := json.Marshal(data)
	return marshal
}

func ExtractIP(addr string) string {
	parts := strings.Split(addr, ":")
	return parts[0]
}

func Reverse(s []byte) {
	for i := 0; i < len(s)/2; i++ {
		j := len(s) - i - 1
		s[i], s[j] = s[j], s[i]
	}
}

func ExceptionCatch() {
	if err := recover(); err != nil {
		trace_log.ErrorLogger.Error("Exception:", err, " \n", string(debug.Stack()))
	}
}
